package com.sky.service.impl;


import com.sky.context.ThreadLocalContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Morpeko
 * @description 针对表【shopping_cart(购物车)】的数据库操作Service实现
 * @createDate 2024-08-16 16:10:00
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加到购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //0.准备查询购物车信息用的ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//拷贝dto的属性到ShoppingCart对象中
        shoppingCart.setUserId(ThreadLocalContext.getCurrentLocalUserId());//设置用户id

        //1.根据当前微信登入的用户id和套餐/果蔬id -->查询购物车是否存在该商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCartList(shoppingCart);
        //2.如果不存在该商品，则新增商品到购物车
        if (shoppingCartList.size() == 0 || shoppingCartList == null) {

            //3.判断添加到购物车的商品是套餐还是果蔬
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (setmealId != null) {
                //如果是套餐，则添加套餐到购物车
                //todo 根据套餐id查询套餐信息
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmeal.getPrice());//设置商品价格
                shoppingCart.setImage(setmeal.getImage());//设置商品图片
                shoppingCart.setNumber(1);//设置商品数量
                shoppingCart.setName(setmeal.getName());//设置商品名称
                shoppingCart.setCreateTime(LocalDateTime.now());//设置添加到购物车的时间

            } else {
                //否则是果蔬，则添加果蔬到购物车
                //todo 根据果蔬id查询果蔬信息
                DishVO dishVO = dishMapper.selectDishById(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(dishVO.getPrice());//设置商品价格
                shoppingCart.setImage(dishVO.getImage());//设置商品图片
                shoppingCart.setNumber(1);//设置商品数量
                shoppingCart.setName(dishVO.getName());//设置商品名称
                shoppingCart.setCreateTime(LocalDateTime.now());//设置添加到购物车的时间
            }

            //业务实现:添加到购物车:把购物车信息插入到数据库中
            // 使用了useGeneratedKeys=true,keyProperty="id"可以返回新增的id,封装到shoppingCart对象中
            shoppingCartMapper.insert(shoppingCart);
        } else {
            //4.如果存在该商品，则更新商品数量
            //更新购物车中的商品数量,数量+1

            shoppingCart.setId(shoppingCartList.get(0).getId());
            //取出查询到的购物车信息中的购物车id,设置到shoppingCart对象中,用于更新数量
            shoppingCartMapper.updateNumber(shoppingCart.getId());
        }


    }

    /**
     * 获取用户的购物车列表,用于前端小程序显示
     *
     * @return
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {
        //1.获取当前用户的id
        Long userId = ThreadLocalContext.getCurrentLocalUserId();
        //2.根据用户id查询购物车列表
        //业务实现:获取用户的购物车列表
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectShoppingCartList(ShoppingCart.builder()
                .userId(userId)
                .build());
        return shoppingCarts;
    }

    /**
     * 根据用户ID删除用户购物车中的所有商品
     */
    @Override
    public void cleanShoppingCart() {
        //获取当前用户的id,根据id删除购物车中的所有商品
        shoppingCartMapper.deleteByUserId(ThreadLocalContext.getCurrentLocalUserId());

    }

    /**
     * 减少/删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1.把dto中的信息封装到shoppingCart对象中
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(ThreadLocalContext.getCurrentLocalUserId());//设置用户id
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);//属性拷贝
        //2.业务实现:查看购物车中该商品的数量是否大于1,如果大于1,则数量-1,如果等于1,则删除该商品
        //根据前端传递的参数动态查询购物车信息
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectShoppingCartList(shoppingCart);
        shoppingCart=shoppingCarts.get(0);//取出购物车中商品信息,购物车中只可能有一条记录
        //因为是根据用户ID和套餐/果蔬ID查询的,所以肯定是只有一条记录
        if(shoppingCart.getNumber()>1){
            //如果数量大于1,则数量-1
            shoppingCart.setNumber(shoppingCart.getNumber()-1);//设置减少后的数量
            shoppingCartMapper.subNumber(shoppingCart);//更新购物车中的数量
        }else if(shoppingCart.getNumber()==1){
            //如果数量等于1,则删除该商品
            shoppingCartMapper.deleteById(shoppingCart.getId());//删除用户购物车中的该商品
        }
    }
}




